示例程序
这里列出了所有的示例程序:
ChunkData.cpp
获取Chunk信息
该示例程序说明了如何打开ChunkData功能、如何配置ChunkData以及如何获取这些ChunkData信息。ChunkData例如Timestamp 、Exposure 等。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bExit = false;
unsigned int g_nPayloadSize = 0;
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
return NULL;
}
unsigned char * pData = (
unsigned char *)malloc(
sizeof(
unsigned char) * stParam.
nCurValue);
if (NULL == pData)
{
return NULL;
}
while(1)
{
if(g_bExit)
{
break;
}
if (nRet == MV_OK)
{
printf("Get One Frame: Chunk_ExposureTime[%f], Chunk_SecondCount[%d], Chunk_CycleCount[%d], Chunk_CycleOffset[%d], nFrameNum[%d]\n",
}
else{
printf("No data[%x]\n", nRet);
}
}
free(pData);
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Set Chunk Mode fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Set Exposure Chunk fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Set Chunk Enable fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Set Timestamp Chunk fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Set Chunk Enable fail! nRet [0x%x]\n", nRet);
break;
}
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
PressEnterToExit();
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}
ConnectSpecCamera.cpp
通过ip地址连接相机
该示例程序说明了如何通过ip地址连接网口相机。通过输入需要连接的相机ip(Camera Ip)和相机对应的网卡ip(Export Ip)来连接相机,输入的格式为xx.xx.xx.xx。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bExit = false;
unsigned int g_nPayloadSize = 0;
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
unsigned char * pData = (unsigned char *)malloc(sizeof(unsigned char) * (g_nPayloadSize));
if (NULL == pData)
{
return NULL;
}
unsigned int nDataSize = g_nPayloadSize;
while(1)
{
if(g_bExit)
{
break;
}
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
}
else
{
printf("No data[0x%x]\n", nRet);
break;
}
}
free(pData);
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
printf("Please input Current Camera Ip : ");
char nCurrentIp[128];
scanf("%s", &nCurrentIp);
printf("Please input Net Export Ip : ");
char nNetExport[128];
scanf("%s", &nNetExport);
unsigned int nIp1, nIp2, nIp3, nIp4, nIp;
sscanf(nCurrentIp, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
sscanf(nNetExport, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
do
{
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet[0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
printf("Press a key to stop grabbing.\n");
PressEnterToExit();
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Close Device fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
handle = NULL;
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit.\n");
return 0;
}
Display.cpp
图像显示
演示了图像采集并使用图像显示接口进行显示。
#include <X11/Xlib.h>
#include <assert.h>
#include "math.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define NIL (0)
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
sleep(1);
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
Window w;
Display *dpy;
memset(&w, 0, sizeof(Window));
dpy = NULL;
dpy = XOpenDisplay(NIL);
if (NULL == dpy)
{
printf("please run with screan environment\n");
return -1;
}
int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
752, 480, 0, 0xffff00ff, 0xff00ffff);
XSelectInput(dpy, w, StructureNotifyMask);
XMapWindow(dpy, w);
GC gc = XCreateGC(dpy, w, 0, NIL);
XSetForeground(dpy, gc, whiteColor);
for(;;)
{
XEvent e;
XNextEvent(dpy, &e);
if (e.type == MapNotify)
{
break;
}
}
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_Display fail! nRet [%x]\n", nRet);
break;
}
printf("Display succeed\n");
PressEnterToExit();
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
}while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}
Events.cpp
获取相机事件
该示例程序说明了如何配置相机事件功能、如何注册事件回调以及如何在事件回调函数中处理获取到的事件信息。Event事件例如:ExposureEnd。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
{
if (pEventInfo)
{
printf("EventName[%s], EventID[%d], BlockId[%lld], Timestamp[%lld]\n",
}
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
{
for (
unsigned int i = 0; i < stDeviceList.
nDeviceNum; i++)
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Set Event Selector fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Set Event Notification fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Register Event CallBack fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
PressEnterToExit();
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Close Device fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit.\n");
return 0;
}
ForceIP.cpp
设置相机静态ip
该示例程序说明了如何设置网口相机静态ip。通过输入相机ip,子网掩码(NetMask),默认网关(DefaultWay)来设置相机ip地址。输入的格式为xx.xx.xx.xx。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void PressEnterToExit(void)
{
int c;
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
bool ConvertToHexIp(unsigned int *nHexIP, unsigned int *nDecIP, char c)
{
if ( nDecIP[0] < 0 || nDecIP[0] > 255
|| nDecIP[1] < 0 || nDecIP[1] > 255
|| nDecIP[2] < 0 || nDecIP[2] > 255
|| nDecIP[3] < 0 || nDecIP[3] > 255
|| c != '\n')
{
return false;
}
*nHexIP = (nDecIP[0] << 24) + (nDecIP[1] << 16) + (nDecIP[2] << 8) + nDecIP[3];
return true;
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
unsigned int nIP[4] = {0};
char c = '\0';
unsigned int nIpAddr = 0, nNetWorkMask = 0, nDefaultGateway = 0;
do
{
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
printf("Please input ip, example: 192.168.1.100\n");
int ch;
if ( 5 != scanf("%d.%d.%d.%d%c", &nIP[0], &nIP[1], &nIP[2], &nIP[3], &c) )
{
printf("input count error\n");
break;
}
if (!ConvertToHexIp(&nIpAddr, nIP, c))
{
printf("input IpAddr format is not correct\n");
break;
}
printf("Please input NetMask, example: 255.255.255.0\n");
if ( 5 != scanf("%d.%d.%d.%d%c", &nIP[0], &nIP[1], &nIP[2], &nIP[3], &c) )
{
printf("input count error\n");
break;
}
if (!ConvertToHexIp(&nNetWorkMask, nIP, c))
{
printf("input NetMask format is not correct\n");
break;
}
printf("Please input DefaultWay, example: 192.168.1.1\n");
if ( 5 != scanf("%d.%d.%d.%d%c", &nIP[0], &nIP[1], &nIP[2], &nIP[3], &c) )
{
printf("input count error\n");
break;
}
if (!ConvertToHexIp(&nDefaultGateway, nIP, c))
{
printf("input DefaultWay format is not correct\n");
break;
}
if (MV_OK != nRet)
{
printf("MV_GIGE_ForceIpEx fail! nRet [%x]\n", nRet);
break;
}
printf("set IP succeed\n");
PressEnterToExit();
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}
Grab_ImageCallback.cpp
通过回调获取相机图像
该示例程序说明了如何通过回调的方式获取相机图像。创建句柄,打开相机,注册回调函数,开始取流,在回调函数中可以获取到图像相关信息,停止取流,关闭相机,销毁句柄。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
{
if (pFrameInfo)
{
printf("GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
}
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_RegisterImageCallBackEx fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
PressEnterToExit();
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}
GrabImage.cpp
获取相机图像
该示例程序说明了如何通过主动取图的方式获取相机图像。创建句柄,打开相机,开始取流,创建取流线程(用户分配采集buffer, MV_CC_GetOneFrameTimeout() 取图(拷贝)),停止取流,关闭相机,销毁句柄。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bExit = false;
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
return NULL;
}
unsigned char * pData = (
unsigned char *)malloc(
sizeof(
unsigned char) * stParam.
nCurValue);
if (NULL == pData)
{
return NULL;
}
while(1)
{
if(g_bExit)
{
break;
}
if (nRet == MV_OK)
{
printf("GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
}
else{
printf("No data[%x]\n", nRet);
}
}
free(pData);
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
PressEnterToExit();
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}
GrabImage_HighPerformance.cpp
高性能获取相机图像
该示例程序说明了高效率的主动取图方式。创建句柄,打开相机,开始取流,创建取流线程( MV_CC_GetImageBuffer() 获取图像地址, MV_CC_FreeImageBuffer() 释放图像缓存),停止取流,关闭相机,销毁句柄。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bExit = false;
unsigned int g_nPayloadSize = 0;
void WaitForKeyPress(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
while(1)
{
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
}
else
{
printf("No data[0x%x]\n", nRet);
}
{
if(nRet != MV_OK)
{
printf("Free Image Buffer fail! nRet [0x%x]\n", nRet);
}
}
if(g_bExit)
{
break;
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
{
for (
unsigned int i = 0; i < stDeviceList.
nDeviceNum; i++)
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index:");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!", nPacketSize);
}
}
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
printf("Press a key to stop grabbing.\n");
WaitForKeyPress();
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("Press a key to exit.\n");
WaitForKeyPress();
return 0;
}
GrabMultipleCamera.cpp
多相机取流
该示例程序说明了如何实现多相机取图。示例中通过创建多个相机句柄handle的方式以区分相机。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#define CAMERA_NUM 2
bool g_bExit = false;
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
char camSerialNumber[256] = {0};
if (MV_OK == nRet)
{
}
else
{
printf("Get DeviceUserID Failed! nRet = [%x]\n", nRet);
}
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
return NULL;
}
unsigned char * pData = (
unsigned char *)malloc(
sizeof(
unsigned char) * stParam.
nCurValue);
if (NULL == pData)
{
return NULL;
}
while(1)
{
if(g_bExit)
{
break;
}
if (nRet == MV_OK)
{
printf("Cam Serial Number[%s]:GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
}
else
{
printf("cam[%s]:Get One Frame failed![%x]\n", camSerialNumber, nRet);
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle[CAMERA_NUM] = {NULL};
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
return -1;
}
unsigned int nIndex = 0;
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
return -1;
}
{
printf(
"only have %d camera\n", stDeviceList.
nDeviceNum);
return -1;
}
printf("Start %d camera Grabbing Image test\n", CAMERA_NUM);
for(int i = 0; i < CAMERA_NUM; i++)
{
printf("Please Input Camera Index: ");
scanf("%d", &nIndex);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
return -1;
}
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
return -1;
}
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
}
for(int i = 0; i < CAMERA_NUM; i++)
{
if (MV_OK != nRet)
{
printf("Cam[%d]: MV_CC_SetTriggerMode fail! nRet [%x]\n", i, nRet);
}
if (MV_OK != nRet)
{
printf("Cam[%d]: MV_CC_StartGrabbing fail! nRet [%x]\n",i, nRet);
return -1;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle[i]);
if (nRet != 0)
{
printf("Cam[%d]: thread create failed.ret = %d\n",i, nRet);
return -1;
}
}
PressEnterToExit();
for(int i = 0; i < CAMERA_NUM; i++)
{
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
return -1;
}
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
return -1;
}
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
return -1;
}
}
printf("exit\n");
return 0;
}
ImageProcess.cpp
图像处理
该示例程序说明了如何实现图像像素格式转换和图像保存两个功能。在获取到图像数据后可以使用 MV_CC_ConvertPixelType() 进行数据格式转换,使用 MV_CC_SaveImageEx2() 进行图像保存。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
unsigned char * pData = NULL;
unsigned char *pDataForRGB = NULL;
unsigned char *pDataForSaveImage = NULL;
do
{
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
pData = (
unsigned char *)malloc(
sizeof(
unsigned char) * stParam.
nCurValue);
if (NULL == pData)
{
break;
}
if (nRet == MV_OK)
{
printf("Now you GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n\n",
printf("input 0 to do nothing, 1 to convert RGB, 2 to save as BMP\n");
int nInput = 0;
scanf("%d", &nInput);
switch (nInput)
{
case 0:
{
break;
}
case 1:
{
pDataForRGB = (
unsigned char*)malloc(stImageInfo.
nWidth * stImageInfo.
nHeight * 4 + 2048);
if (NULL == pDataForRGB)
{
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_ConvertPixelType fail! nRet [%x]\n", nRet);
break;
}
FILE* fp = fopen("AfterConvert_RGB.raw", "wb");
if (NULL == fp)
{
printf("fopen failed\n");
break;
}
fwrite(pDataForRGB, 1, stConvertParam.
nDstLen, fp);
fclose(fp);
printf("convert succeed\n");
break;
}
case 2:
{
pDataForSaveImage = (
unsigned char*)malloc(stImageInfo.
nWidth * stImageInfo.
nHeight * 4 + 2048);
if (NULL == pDataForSaveImage)
{
break;
}
stSaveParam.
pData = pData;
if(MV_OK != nRet)
{
printf("failed in MV_CC_SaveImage,nRet[%x]\n", nRet);
break;
}
FILE* fp = fopen("image.bmp", "wb");
if (NULL == fp)
{
printf("fopen failed\n");
break;
}
fwrite(pDataForSaveImage, 1, stSaveParam.
nImageLen, fp);
fclose(fp);
printf("save image succeed\n");
break;
}
default:
break;
}
}
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
if (pData)
{
free(pData);
pData = NULL;
}
if (pDataForRGB)
{
free(pDataForRGB);
pDataForRGB = NULL;
}
if (pDataForSaveImage)
{
free(pDataForSaveImage);
pDataForSaveImage = NULL;
}
PressEnterToExit();
printf("exit\n");
return 0;
}
MultiCast.cpp
设置组播模式
该示例程序说明了如何设置传输模式为组播模式。通过询问用户启动多播控制应用程序或者多播监控应用程序,然后根据用户输入,以对应的访问权限打开相机,最后通过 MV_GIGE_SetTransmissionType() 设置组播模式。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
bool g_bExit = false;
unsigned int g_nPayloadSize = 0;
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
unsigned char * pData = (unsigned char *)malloc(sizeof(unsigned char) * (g_nPayloadSize));
if (pData == NULL)
{
return 0;
}
unsigned int nDataSize = g_nPayloadSize;
while(1)
{
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
}
else
{
printf("No data[0x%x]\n", nRet);
}
if(g_bExit)
{
break;
}
}
free(pData);
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
{
for (
unsigned int i = 0; i < stDeviceList.
nDeviceNum; i++)
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
int c;
while ( (c = getchar()) != '\n' && c != EOF );
char key;
printf("Start multicast sample in (c)ontrol or in (m)onitor mode? (c/m)\n");
scanf("%c", &key);
if((key != 'c') && (key != 'm') && (key != 'C') && (key != 'M'))
{
printf("Input error\n");
break;
}
bool monitorMode = (key == 'm') || (key == 'M');
if (monitorMode)
{
}
else
{
}
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
char strIp[] = "239.192.1.1";
unsigned int nIp1, nIp2, nIp3, nIp4, nIp;
sscanf(strIp, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
if (MV_OK != nRet)
{
printf("Set Transmission Type fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
pthread_t nThreadID = 0;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
break;
}
printf("Press a key to stop grabbing.\n");
PressEnterToExit();
g_bExit = true;
sleep(1);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit.\n");
return 0;
}
ParametrizeCamera_FileAccess.cpp
导入或导出文件到相机
该示例程序说明了如何导入文件到相机以及如何导出相机文件。演示了通过 MV_CC_FileAccessRead() 从设备读取文件(例如UserSet1.bin);通过 MV_CC_FileAccessWrite() 将文件(例如UserSet1.bin)写入相机;以及通过 MV_CC_GetFileAccessProgress() 获取文件的存取进度。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
unsigned int g_nMode = 0;
int g_nRet = MV_OK;
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
static void* ProgressThread(void* pUser)
{
int nRet = MV_OK;
while(1)
{
printf(
"State = 0x%x,Completed = %ld,Total = %ld\r\n",nRet,stFileAccessProgress.
nCompleted,stFileAccessProgress.
nTotal);
{
break;
}
usleep(50000);
}
return 0;
}
static void* FileAccessThread(void* pUser)
{
if (1 == g_nMode)
{
if (MV_OK != g_nRet)
{
printf("File Access Read fail! nRet [0x%x]\n", g_nRet);
}
}
else if (2 == g_nMode)
{
if (MV_OK != g_nRet)
{
printf("File Access Write fail! nRet [0x%x]\n", g_nRet);
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
{
for (
unsigned int i = 0; i < stDeviceList.
nDeviceNum; i++)
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
g_nMode = 1;
printf("Read to file.\n");
pthread_t nReadHandle;
nRet = pthread_create(&nReadHandle, NULL ,FileAccessThread , handle);
if (nRet != 0)
{
break;
}
usleep(5000);
pthread_t nReadProcessHandle;
nRet = pthread_create(&nReadProcessHandle, NULL ,ProgressThread , handle);
if (nRet != 0)
{
break;
}
void *statusRead;
void *statusReadProcess;
pthread_join(nReadHandle, &statusRead);
pthread_join(nReadProcessHandle, &statusReadProcess);
if (MV_OK == g_nRet)
{
printf("File Access Read Success!\n");
}
printf("\n");
g_nMode = 2;
printf("Write from file.\n");
pthread_t nWriteHandle;
nRet = pthread_create(&nWriteHandle, NULL ,FileAccessThread , handle);
if (nRet != 0)
{
break;
}
usleep(5000);
pthread_t nWriteProgressHandle;
nRet = pthread_create(&nWriteProgressHandle, NULL ,ProgressThread , handle);
if (nRet != 0)
{
break;
}
void *statusWrite;
void *statusWriteProcess;
pthread_join(nWriteHandle, &statusWrite);
pthread_join(nWriteProgressHandle, &statusWriteProcess);
if (MV_OK == g_nRet)
{
printf("File Access Write Success!\n");
}
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit.\n");
return 0;
}
ParametrizeCamera_LoadAndSave.cpp
导入或导出相机属性
该示例程序说明了如何导入和导出相机的属性树文件。演示了通过 MV_CC_FeatureSave() 将相机属性导出到文件(例如FeatureFile.ini)中;通过 MV_CC_FeatureLoad() 从文件(例如FeatureFile.ini)中导入相机的属性树。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
{
for (
unsigned int i = 0; i < stDeviceList.
nDeviceNum; i++)
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
printf("Start export the camera properties to the file\n");
printf("Wait......\n");
if (MV_OK != nRet)
{
printf("Save Feature fail! nRet [0x%x]\n", nRet);
break;
}
printf("Finish export the camera properties to the file\n\n");
printf("Start import the camera properties from the file\n");
printf("Wait......\n");
if (MV_OK != nRet)
{
printf("Load Feature fail! nRet [0x%x]\n", nRet);
break;
}
printf("Finish import the camera properties from the file\n");
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit.\n");
return 0;
}
ReconnectDemo.cpp
相机重连
该示例程序说明了相机掉线后,心跳时间内重新连接后的操作,通过在打开相机后注册异常回调函数来监测相机掉线异常状态。重连后先进行停止取流、关闭设备、销毁句柄操作后重新连接相机。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void* g_hHandle = NULL;
bool g_bConnect = false;
char g_strSerialNumber[64] = {0};
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
void __stdcall cbException(
unsigned int nMsgType,
void* pUser)
{
printf("Device disconnect!\n");
g_bConnect = false;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
return NULL;
}
unsigned char * pData = (
unsigned char *)malloc(
sizeof(
unsigned char) * stParam.
nCurValue);
if (NULL == pData)
{
return NULL;
}
while(1)
{
if(!g_bConnect)
{
break;
}
if (nRet == MV_OK)
{
printf("GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
}
else
{
printf("no data[%x]\n", nRet);
}
}
free(pData);
return 0;
}
static void* ReconnectProcess(void* pUser)
{
int nRet = MV_OK;
while(1)
{
if (true == g_bConnect)
{
sleep(1);
continue;
}
g_hHandle = NULL;
printf("connecting...\n");
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
continue;
}
unsigned int nIndex = -1;
{
{
if (NULL == pDeviceInfo)
{
continue;
}
{
{
nIndex = i;
break;
}
}
{
{
nIndex = i;
break;
}
}
}
}
else
{
continue;
}
if (-1 == nIndex)
{
continue;
}
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
continue;
}
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
continue;
}
g_bConnect = true;
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
if (MV_OK != nRet)
{
printf("MV_CC_RegisterExceptionCallBack fail! nRet [%x]\n", nRet);
continue;
}
printf("connect succeed\n");
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
continue;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , NULL);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
continue;
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
unsigned int nSelectNum = 0;
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
return -1;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
return -1;
}
printf("Please Intput camera index: ");
scanf("%d", &nSelectNum);
{
printf("Intput error!\n");
return -1;
}
{
}
{
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL, ReconnectProcess, NULL);
if (nRet != 0)
{
printf("thread create failed nRet = %d\n",nRet);
return -1;
}
PressEnterToExit();
g_bConnect = false;
printf("exit\n");
return 0;
}
SetIO.cpp
设置相机IO
该示例程序说明了在打开设备后如何设置和获取设备IO状态。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK == nRet)
{
printf(
"stLineSelector current value:%d\n", stLineSelector.
nCurValue);
printf(
"supported stLineSelector number:%d\n", stLineSelector.
nSupportedNum);
{
printf(
"supported stLineSelector [%d]:%d\n", i, stLineSelector.
nSupportValue[i]);
}
printf("\n");
}
else
{
printf("get stLineSelector failed! nRet [%x]\n\n", nRet);
}
unsigned int nLineSelector = 0;
printf("please input the LineSelector to set: ");
scanf("%d", &nLineSelector);
if (MV_OK == nRet)
{
printf("set LineSelector OK!\n\n");
}
else
{
printf("set LineSelector failed! nRet [%x]\n\n", nRet);
}
if (MV_OK == nRet)
{
printf(
"stLineMode current value:%d\n", stLineMode.
nCurValue);
printf(
"supported stLineSelector number:%d\n", stLineMode.
nSupportedNum);
{
printf(
"supported stLineSelector [%d]:%d\n", i, stLineMode.
nSupportValue[i]);
}
printf("\n");
}
else
{
printf("get stLineMode failed! nRet [%x]\n\n", nRet);
}
unsigned int nLineMode = 0;
printf("please input the LineMode to set:");
scanf("%d", &nLineMode);
if (MV_OK == nRet)
{
printf("set LineMode OK!\n\n");
}
else
{
printf("set LineMode failed! nRet [%x]\n\n", nRet);
}
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}
SetParam.cpp
设置相机参数
该示例程序说明了如何使用通用接口设置相机参数,包括Int、Float、Enum、Bool等类型节点的设置。
#include <stdio.h>
#include <string.h>
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK == nRet)
{
printf(
"height current value:%d\n", stHeight.
nCurValue);
printf(
"height max value:%d\n", stHeight.
nMax);
printf(
"height min value:%d\n", stHeight.
nMin);
printf(
"height increment value:%d\n\n", stHeight.
nInc);
}
else
{
printf("get height failed! nRet [%x]\n\n", nRet);
}
unsigned int nHeightValue = 0;
printf("please input the height to set:");
scanf("%d", &nHeightValue);
if (MV_OK == nRet)
{
printf("set height OK!\n\n");
}
else
{
printf("set height failed! nRet [%x]\n\n", nRet);
}
if (MV_OK == nRet)
{
printf(
"exposure time current value:%f\n", stExposureTime.
fCurValue);
printf(
"exposure time max value:%f\n", stExposureTime.
fMax);
printf(
"exposure time min value:%f\n\n", stExposureTime.
fMin);
}
else
{
printf("get exposure time failed! nRet [%x]\n\n", nRet);
}
float fExposureTime = 0.0f;
printf("please input the exposure time to set: ");
scanf("%f", &fExposureTime);
if (MV_OK == nRet)
{
printf("set exposure time OK!\n\n");
}
else
{
printf("set exposure time failed! nRet [%x]\n\n", nRet);
}
if (MV_OK == nRet)
{
printf(
"TriggerMode current value:%d\n", stTriggerMode.
nCurValue);
printf(
"supported TriggerMode number:%d\n", stTriggerMode.
nSupportedNum);
{
printf(
"supported TriggerMode [%d]:%d\n", i, stTriggerMode.
nSupportValue[i]);
}
printf("\n");
}
else
{
printf("get TriggerMode failed! nRet [%x]\n\n", nRet);
}
unsigned int nTriggerMode = 0;
printf("please input the TriggerMode to set:");
scanf("%d", &nTriggerMode);
if (MV_OK == nRet)
{
printf("set TriggerMode OK!\n\n");
}
else
{
printf("set TriggerMode failed! nRet [%x]\n\n", nRet);
}
bool bGetBoolValue = false;
if (MV_OK == nRet)
{
if (0 != bGetBoolValue)
{
printf("ReverseX current is true\n\n");
}
else
{
printf("ReverseX current is false\n\n");
}
}
int nSetBoolValue;
bool bSetBoolValue;
printf("please input the ReverseX to set(bool): ");
scanf("%d", &nSetBoolValue);
if (0 != nSetBoolValue)
{
bSetBoolValue = true;
}
else
{
bSetBoolValue = false;
}
if (MV_OK == nRet)
{
printf("Set ReverseX OK!\n\n");
}
else
{
printf("Set ReverseX Failed! nRet = [%x]\n\n", nRet);
}
if (MV_OK == nRet)
{
printf(
"Get DeviceUserID [%s]\n\n", stStringValue.
chCurValue);
}
else
{
printf("Get DeviceUserID Failed! nRet = [%x]\n\n", nRet);
}
unsigned char strValue[256];
printf("please input the DeviceUserID to set(string):");
scanf("%s", strValue);
if (MV_OK == nRet)
{
printf("Set DeviceUserID OK!\n\n");
}
else
{
printf("Set DeviceUserID Failed! nRet = [%x]\n\n", nRet);
}
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}
Trigger_Image.cpp
触发模式获取相机图像
该示例程序说明了触发模式下通过软触发主动取图的方式获取图像。创建句柄,打开相机,设置触发模式,设置触发源(软触发),开始取流,创建取流线程(发送软触发command命令, MV_CC_GetOneFrameTimeout() 取图),停止取流,关闭相机,销毁句柄。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bExit = false;
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
return NULL;
}
unsigned char * pData = (
unsigned char *)malloc(
sizeof(
unsigned char) * stParam.
nCurValue);
if (NULL == pData)
{
return NULL;
}
while(1)
{
if(MV_OK != nRet)
{
printf("failed in TriggerSoftware[%x]\n", nRet);
}
if (nRet == MV_OK)
{
printf("GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
}
else
{
printf("Get One Frame failed![%x]\n", nRet);
}
if(g_bExit)
{
break;
}
}
if (pData)
{
free(pData);
pData = NULL;
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
if (MV_OK != nRet)
{
printf("set AcquisitionFrameRateEnable fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerSource fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
PressEnterToExit();
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}
Trigger_ImageCallback.cpp
触发模式下通过回调获取相机图像
该示例程序说明了通过软触发回调取图的方式获取图像。创建句柄,打开相机,设置触发模式,设置触发源(软触发),注册抓图回调函数,开始取流,在回调函数中可以获取通过软触发得到的图像信息,停止取流,关闭相机,销毁句柄。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bIsGetImage = true;
bool g_bExit = false;
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
{
if (pFrameInfo)
{
printf("GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
g_bIsGetImage = true;
}
}
static void* WorkThread(void* pUser)
{
while(1)
{
if(g_bExit)
{
break;
}
if (true == g_bIsGetImage)
{
if(MV_OK != nRet)
{
printf("failed in TriggerSoftware[%x]\n", nRet);
}
else
{
g_bIsGetImage = false;
}
}
else
{
continue;
}
}
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
if (MV_OK != nRet)
{
printf("set AcquisitionFrameRateEnable fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerSource fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_RegisterImageCallBackEx fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
PressEnterToExit();
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}